package tankwar;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class Panel  extends JFrame {
	int width=1000;
	int height=610;//窗口长宽
	
	
	//窗口启动
	Image select =Toolkit.getDefaultToolkit().getImage("images/ourtank.gif");
	int y=150;
	int state=0;
	int a=1;//防止在回车前进入游戏界面\
	//定义玩家1
	PlayerOne playerOne=new PlayerOne("images/PlayerOne/p1tankU.gif",225,510,this,
			"images/PlayerOne/p1tankU.gif","images/PlayerOne/p1tankL.gif",
			"images/PlayerOne/p1tankR.gif","images/PlayerOne/p1tankD.gif");
	
	//定义双缓存图片
	Image offScreemImage=null;
	
	public void launch() {
		//标题
		setTitle("坦克大战");
		setSize(width,height);
		//屏幕居中
		setLocationRelativeTo(null);
		//添加关闭事件
		setDefaultCloseOperation(3);
		setResizable(false);
		setVisible(true);
		//添加键盘监视器
		this.addKeyListener(new Panel.KeyMonitor());
		//重绘
		while(true) {
			repaint();
			try {
				Thread.sleep(25);
			}catch(Exception e) {
				e.printStackTrace();  
			} 
			
		}
	}
//	@Override
	public void paint(Graphics g) {
		//创建和窗口一样大小的图片
		if(offScreemImage==null) {
			offScreemImage=this.createImage(width,height);
		}
		//获得该图片的画布
		Graphics gImage=offScreemImage.getGraphics();
		
		//设置画笔颜色
		gImage.setColor(Color.black);
		//绘制实心矩形
		gImage.fillRect(0,0,width,height);
		gImage.setColor(Color.blue);
		gImage.setFont(new Font("仿宋",Font.BOLD,50));
		if(state==0) {
			//添加文字
			gImage.drawString("选择游戏模式", 220, 100);
			gImage.drawString("单人模式", 220, 200);
			gImage.drawString("双人模式", 220, 300);
			gImage.drawString("按1、2进行模式选择", 220, 400);
			//绘制指针
			gImage.drawImage(select, 160, y,null);	
		}
		else if(state==1||state==2) {
			gImage.drawString("游戏开始", 220, 100);
			if(state==1) {
				gImage.drawString("单人模式",220,200);
			}
			else {
				gImage.drawString("双人模式",220,200);
			}
			
			//添加游戏元素
			playerOne.paintSelf(gImage);
		}
		g.drawImage(offScreemImage, 0,0,null);
			 
	}
	//键盘监视器
	class KeyMonitor extends KeyAdapter{
		public void keyPressed(KeyEvent e) {
			System.out.println(e.getKeyChar());
			int key=e.getKeyCode();
			switch(key) {
			case KeyEvent.VK_1:
				a=1;
				y=150;
				break;
			case KeyEvent.VK_2:
				a=2;
				y=250; 
				break;
			case KeyEvent.VK_ENTER:
				state=a;
				break;
			}
		}
	}
	public static void main(String[] args) {
		Panel ks=new Panel();
		ks.launch();
	}
}
